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

Function character_length

datafusion/spark/src/function/string/length.rs:134–177  ·  view source on GitHub ↗
(array: &V)

Source from the content-addressed store, hash-verified

132}
133
134fn character_length<'a, V>(array: &V) -> datafusion_common::Result<ArrayRef>
135where
136 V: StringArrayType<'a>,
137{
138 // String characters are variable length encoded in UTF-8, counting the
139 // number of chars requires expensive decoding, however checking if the
140 // string is ASCII only is relatively cheap.
141 // If strings are ASCII only, count bytes instead.
142 let is_array_ascii_only = array.is_ascii();
143 let nulls = array.nulls().cloned();
144 let array = {
145 if is_array_ascii_only {
146 let values: Vec<_> = (0..array.len())
147 .map(|i| {
148 // Safety: we are iterating with array.len() so the index is always valid
149 let value = unsafe { array.value_unchecked(i) };
150 value.len() as i32
151 })
152 .collect();
153 PrimitiveArray::<Int32Type>::new(values.into(), nulls)
154 } else {
155 let values: Vec<_> = (0..array.len())
156 .map(|i| {
157 // Safety: we are iterating with array.len() so the index is always valid
158 if array.is_null(i) {
159 i32::default()
160 } else {
161 let value = unsafe { array.value_unchecked(i) };
162 if value.is_empty() {
163 i32::default()
164 } else if value.is_ascii() {
165 value.len() as i32
166 } else {
167 value.chars().count() as i32
168 }
169 }
170 })
171 .collect();
172 PrimitiveArray::<Int32Type>::new(values.into(), nulls)
173 }
174 };
175
176 Ok(Arc::new(array))
177}
178
179fn byte_length<'a, V>(array: &V) -> datafusion_common::Result<ArrayRef>
180where

Callers 2

mock_session_stateFunction · 0.50
test_fn_character_lengthFunction · 0.50

Calls 10

newFunction · 0.85
collectMethod · 0.80
clonedMethod · 0.45
nullsMethod · 0.45
mapMethod · 0.45
lenMethod · 0.45
intoMethod · 0.45
is_nullMethod · 0.45
is_emptyMethod · 0.45
countMethod · 0.45

Tested by 2

mock_session_stateFunction · 0.40
test_fn_character_lengthFunction · 0.40

Used in the wild real call sites across dependent graphs

searching dependent graphs…