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

Function string_view_trim

datafusion/functions/src/string/common.rs:144–236  ·  view source on GitHub ↗

Applies the trim function to the given string view array(s) and returns a new string view array with the trimmed values. Pre-computes the pattern characters once for scalar patterns to avoid repeated allocations per row.

(args: &[ArrayRef])

Source from the content-addressed store, hash-verified

142/// Pre-computes the pattern characters once for scalar patterns to avoid
143/// repeated allocations per row.
144fn string_view_trim<Tr: Trimmer>(args: &[ArrayRef]) -> Result<ArrayRef> {
145 let string_view_array = as_string_view_array(&args[0])?;
146 let mut views_buf = Vec::with_capacity(string_view_array.len());
147 let mut null_builder = NullBufferBuilder::new(string_view_array.len());
148
149 match args.len() {
150 1 => {
151 // Trim spaces by default
152 for (src_str_opt, raw_view) in string_view_array
153 .iter()
154 .zip(string_view_array.views().iter())
155 {
156 if let Some(src_str) = src_str_opt {
157 let (trimmed, offset) = Tr::trim_ascii_char(src_str, b' ');
158 append_view(&mut views_buf, raw_view, trimmed, offset);
159 null_builder.append_non_null();
160 } else {
161 null_builder.append_null();
162 views_buf.push(0);
163 }
164 }
165 }
166 2 => {
167 let characters_array = as_string_view_array(&args[1])?;
168
169 if characters_array.len() == 1 {
170 // Scalar pattern - pre-compute pattern chars once
171 if characters_array.is_null(0) {
172 return Ok(new_null_array(
173 &DataType::Utf8View,
174 string_view_array.len(),
175 ));
176 }
177
178 let pattern: Vec<char> = characters_array.value(0).chars().collect();
179 for (src_str_opt, raw_view) in string_view_array
180 .iter()
181 .zip(string_view_array.views().iter())
182 {
183 trim_and_append_view::<Tr>(
184 src_str_opt,
185 &pattern,
186 &mut views_buf,
187 &mut null_builder,
188 raw_view,
189 );
190 }
191 } else {
192 // Per-row pattern - must compute pattern chars for each row
193 let mut pattern: Vec<char> = Vec::new();
194 for ((src_str_opt, raw_view), characters_opt) in string_view_array
195 .iter()
196 .zip(string_view_array.views().iter())
197 .zip(characters_array.iter())
198 {
199 if let (Some(src_str), Some(characters)) =
200 (src_str_opt, characters_opt)
201 {

Callers

nothing calls this directly

Calls 15

as_string_view_arrayFunction · 0.85
newFunction · 0.85
append_viewFunction · 0.85
trimFunction · 0.85
collectMethod · 0.80
to_vecMethod · 0.80
lenMethod · 0.45
iterMethod · 0.45
append_nullMethod · 0.45
pushMethod · 0.45
is_nullMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…