MCPcopy Create free account
hub / github.com/apache/datafusion / initcap

Function initcap

datafusion/functions/src/unicode/initcap.rs:151–187  ·  view source on GitHub ↗

Converts the first letter of each word to uppercase and the rest to lowercase. Words are sequences of alphanumeric characters separated by non-alphanumeric characters. Example: ```sql initcap('hi THOMAS') = 'Hi Thomas' ```

(args: &[ArrayRef])

Source from the content-addressed store, hash-verified

149/// initcap('hi THOMAS') = 'Hi Thomas'
150/// ```
151fn initcap<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
152 let string_array = as_generic_string_array::<T>(&args[0])?;
153
154 if string_array.is_ascii() {
155 return Ok(initcap_ascii_array(string_array));
156 }
157
158 let len = string_array.len();
159 let mut builder = GenericStringArrayBuilder::<T>::with_capacity(
160 len,
161 string_array.value_data().len(),
162 );
163
164 let mut container = String::new();
165 let nulls = string_array.nulls().cloned();
166 if let Some(ref n) = nulls {
167 for i in 0..len {
168 if n.is_null(i) {
169 builder.append_placeholder();
170 } else {
171 // SAFETY: not null per check above.
172 let s = unsafe { string_array.value_unchecked(i) };
173 initcap_string(s, &mut container);
174 builder.append_value(&container);
175 }
176 }
177 } else {
178 for i in 0..len {
179 // SAFETY: no null buffer means every index is valid.
180 let s = unsafe { string_array.value_unchecked(i) };
181 initcap_string(s, &mut container);
182 builder.append_value(&container);
183 }
184 }
185
186 Ok(Arc::new(builder.finish(nulls)?) as ArrayRef)
187}
188
189/// Fast path for `Utf8` or `LargeUtf8` arrays that are ASCII-only. We can use a
190/// single pass over the buffer and operate directly on bytes.

Callers 2

test_fn_initcapFunction · 0.85
criterion_benchmarkFunction · 0.85

Calls 10

initcap_ascii_arrayFunction · 0.85
newFunction · 0.85
initcap_stringFunction · 0.85
append_placeholderMethod · 0.80
lenMethod · 0.45
clonedMethod · 0.45
nullsMethod · 0.45
is_nullMethod · 0.45
append_valueMethod · 0.45
finishMethod · 0.45

Tested by 1

test_fn_initcapFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…