Skip to content

Commit f41e6ea

Browse files
committed
test: move lpad/rpad array-path tests to sqllogictest
Per review feedback, the pad.rs unit tests are all expressible in SQL, so they now live in string_literal.slt next to the other lpad/rpad tests.
1 parent 7c5c7d7 commit f41e6ea

2 files changed

Lines changed: 67 additions & 189 deletions

File tree

‎datafusion/functions/src/unicode/pad.rs‎

Lines changed: 0 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -458,192 +458,3 @@ where
458458

459459
Ok(Arc::new(builder.finish()) as ArrayRef)
460460
}
461-
462-
#[cfg(test)]
463-
mod tests {
464-
use std::sync::Arc;
465-
466-
use arrow::array::{
467-
Array, ArrayRef, AsArray, Int64Array, StringArray, StringViewArray,
468-
};
469-
use arrow::datatypes::{DataType, Field};
470-
use datafusion_common::Result;
471-
use datafusion_common::config::ConfigOptions;
472-
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl};
473-
474-
use crate::unicode::lpad::LPadFunc;
475-
use crate::unicode::rpad::RPadFunc;
476-
477-
/// Invokes `func` with every argument passed as an array, so the
478-
/// per-row path is used instead of the scalar fast path.
479-
fn invoke(func: &dyn ScalarUDFImpl, args: Vec<ArrayRef>) -> Result<StringArray> {
480-
let number_rows = args[0].len();
481-
let arg_fields = args
482-
.iter()
483-
.enumerate()
484-
.map(|(idx, arg)| {
485-
Field::new(format!("arg_{idx}"), arg.data_type().clone(), true).into()
486-
})
487-
.collect();
488-
let result = func.invoke_with_args(ScalarFunctionArgs {
489-
args: args.into_iter().map(ColumnarValue::Array).collect(),
490-
arg_fields,
491-
number_rows,
492-
return_field: Field::new("f", DataType::Utf8, true).into(),
493-
config_options: Arc::new(ConfigOptions::default()),
494-
})?;
495-
Ok(result.into_array(number_rows)?.as_string::<i32>().clone())
496-
}
497-
498-
#[test]
499-
fn pad_array_args() -> Result<()> {
500-
let strings: ArrayRef = Arc::new(StringArray::from(vec![
501-
Some("hi"),
502-
Some("josé"),
503-
Some("hello"),
504-
None,
505-
Some("hi"),
506-
Some("hi"),
507-
]));
508-
let lengths: ArrayRef = Arc::new(Int64Array::from(vec![
509-
Some(5),
510-
Some(6),
511-
Some(3),
512-
Some(5),
513-
None,
514-
Some(0),
515-
]));
516-
let fills: ArrayRef = Arc::new(StringArray::from(vec![
517-
Some("xy"),
518-
Some("é"),
519-
Some("x"),
520-
Some("x"),
521-
Some("x"),
522-
Some("x"),
523-
]));
524-
let no_fill = vec![Arc::clone(&strings), Arc::clone(&lengths)];
525-
let with_fill = vec![strings, lengths, fills];
526-
527-
assert_eq!(
528-
invoke(&LPadFunc::new(), no_fill.clone())?,
529-
StringArray::from(vec![
530-
Some(" hi"),
531-
Some(" josé"),
532-
Some("hel"),
533-
None,
534-
None,
535-
Some(""),
536-
])
537-
);
538-
assert_eq!(
539-
invoke(&RPadFunc::new(), no_fill)?,
540-
StringArray::from(vec![
541-
Some("hi "),
542-
Some("josé "),
543-
Some("hel"),
544-
None,
545-
None,
546-
Some(""),
547-
])
548-
);
549-
assert_eq!(
550-
invoke(&LPadFunc::new(), with_fill.clone())?,
551-
StringArray::from(vec![
552-
Some("xyxhi"),
553-
Some("ééjosé"),
554-
Some("hel"),
555-
None,
556-
None,
557-
Some(""),
558-
])
559-
);
560-
assert_eq!(
561-
invoke(&RPadFunc::new(), with_fill)?,
562-
StringArray::from(vec![
563-
Some("hixyx"),
564-
Some("josééé"),
565-
Some("hel"),
566-
None,
567-
None,
568-
Some(""),
569-
])
570-
);
571-
Ok(())
572-
}
573-
574-
#[test]
575-
fn pad_string_view_array_args() -> Result<()> {
576-
let strings: ArrayRef =
577-
Arc::new(StringViewArray::from(vec![Some("hi"), Some("josé"), None]));
578-
let lengths: ArrayRef =
579-
Arc::new(Int64Array::from(vec![Some(4), Some(5), Some(4)]));
580-
let fills: ArrayRef = Arc::new(StringViewArray::from(vec![
581-
Some("ab"),
582-
Some("ñ"),
583-
Some("ab"),
584-
]));
585-
586-
assert_eq!(
587-
invoke(
588-
&LPadFunc::new(),
589-
vec![
590-
Arc::clone(&strings),
591-
Arc::clone(&lengths),
592-
Arc::clone(&fills)
593-
]
594-
)?,
595-
StringArray::from(vec![Some("abhi"), Some("ñjosé"), None])
596-
);
597-
assert_eq!(
598-
invoke(&RPadFunc::new(), vec![strings, lengths, fills])?,
599-
StringArray::from(vec![Some("hiab"), Some("joséñ"), None])
600-
);
601-
Ok(())
602-
}
603-
604-
#[test]
605-
fn pad_partial_unicode_fill() -> Result<()> {
606-
// Only part of the multi-character fill fits in the remaining space.
607-
let strings: ArrayRef = Arc::new(StringArray::from(vec!["josé", "josé"]));
608-
let lengths: ArrayRef = Arc::new(Int64Array::from(vec![6, 9]));
609-
let fills: ArrayRef = Arc::new(StringArray::from(vec!["ñéx", "ñéx"]));
610-
611-
assert_eq!(
612-
invoke(
613-
&LPadFunc::new(),
614-
vec![
615-
Arc::clone(&strings),
616-
Arc::clone(&lengths),
617-
Arc::clone(&fills)
618-
]
619-
)?,
620-
StringArray::from(vec!["ñéjosé", "ñéxñéjosé"])
621-
);
622-
assert_eq!(
623-
invoke(&RPadFunc::new(), vec![strings, lengths, fills])?,
624-
StringArray::from(vec!["joséñé", "joséñéxñé"])
625-
);
626-
Ok(())
627-
}
628-
629-
#[test]
630-
fn pad_array_length_too_large() {
631-
let strings: ArrayRef = Arc::new(StringArray::from(vec!["hi"]));
632-
let lengths: ArrayRef = Arc::new(Int64Array::from(vec![i32::MAX as i64 + 1]));
633-
634-
for (func, name) in [
635-
(&LPadFunc::new() as &dyn ScalarUDFImpl, "lpad"),
636-
(&RPadFunc::new() as &dyn ScalarUDFImpl, "rpad"),
637-
] {
638-
let err = invoke(func, vec![Arc::clone(&strings), Arc::clone(&lengths)])
639-
.unwrap_err()
640-
.strip_backtrace();
641-
assert_eq!(
642-
err,
643-
format!(
644-
"Execution error: {name} requested length 2147483648 too large, maximum allowed length is 2147483647"
645-
)
646-
);
647-
}
648-
}
649-
}

‎datafusion/sqllogictest/test_files/string/string_literal.slt‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,39 @@ true true true
377377
true true true
378378
true true true
379379

380+
# lpad array path where only part of a multi-character fill fits
381+
query TT
382+
SELECT lpad(column1, column2, column3), lpad(arrow_cast(column1, 'Utf8View'), column2, arrow_cast(column3, 'Utf8View'))
383+
FROM (VALUES ('josé', 6, 'ñéx'), ('josé', 9, 'ñéx')) AS t(column1, column2, column3);
384+
----
385+
ñéjosé ñéjosé
386+
ñéxñéjosé ñéxñéjosé
387+
388+
# lpad array path with a Unicode string, a zero length and a NULL length
389+
query T
390+
SELECT lpad(column1, column2) FROM (VALUES ('josé', 6), ('hi', 0), ('hello', 3), ('hi', NULL)) AS t(column1, column2);
391+
----
392+
josé
393+
(empty)
394+
hel
395+
NULL
396+
397+
# lpad array path with different string widths for the input and the fill
398+
query BBB
399+
SELECT
400+
lpad(arrow_cast(column1, 'Utf8View'), column2, arrow_cast(column3, 'LargeUtf8')) IS NOT DISTINCT FROM column4,
401+
lpad(arrow_cast(column1, 'LargeUtf8'), column2, arrow_cast(column3, 'Utf8View')) IS NOT DISTINCT FROM column4,
402+
lpad(arrow_cast(column1, 'Utf8'), column2, arrow_cast(column3, 'Utf8View')) IS NOT DISTINCT FROM column4
403+
FROM (VALUES ('hi', 5, 'xy', 'xyxhi'), ('josé', 5, 'ñ', 'ñjosé'), (NULL, 4, 'ab', NULL)) AS t(column1, column2, column3, column4);
404+
----
405+
true true true
406+
true true true
407+
true true true
408+
409+
# a length column above i32::MAX is an error on the array path
410+
query error lpad requested length 2147483648 too large
411+
SELECT lpad('hi', column1) FROM (VALUES (2147483648), (1)) AS t(column1);
412+
380413
# a large scalar target length skips the scalar fast path
381414
query I
382415
SELECT character_length(lpad('x', 16385, 'a'));
@@ -763,6 +796,40 @@ true true true
763796
true true true
764797
true true true
765798

799+
# rpad array path where only part of a multi-character fill fits
800+
query TT
801+
SELECT rpad(column1, column2, column3), rpad(arrow_cast(column1, 'Utf8View'), column2, arrow_cast(column3, 'Utf8View'))
802+
FROM (VALUES ('josé', 6, 'ñéx'), ('josé', 9, 'ñéx')) AS t(column1, column2, column3);
803+
----
804+
joséñé joséñé
805+
joséñéxñé joséñéxñé
806+
807+
# rpad array path with a Unicode string, a zero length and a NULL length; the
808+
# trailing '|' keeps the padding visible, since trailing whitespace is trimmed
809+
query T
810+
SELECT rpad(column1, column2) || '|' FROM (VALUES ('josé', 6), ('hi', 0), ('hello', 3), ('hi', NULL)) AS t(column1, column2);
811+
----
812+
josé |
813+
|
814+
hel|
815+
NULL
816+
817+
# rpad array path with different string widths for the input and the fill
818+
query BBB
819+
SELECT
820+
rpad(arrow_cast(column1, 'Utf8View'), column2, arrow_cast(column3, 'LargeUtf8')) IS NOT DISTINCT FROM column4,
821+
rpad(arrow_cast(column1, 'LargeUtf8'), column2, arrow_cast(column3, 'Utf8View')) IS NOT DISTINCT FROM column4,
822+
rpad(arrow_cast(column1, 'Utf8'), column2, arrow_cast(column3, 'Utf8View')) IS NOT DISTINCT FROM column4
823+
FROM (VALUES ('hi', 4, 'ab', 'hiab'), ('josé', 5, 'ñ', 'joséñ'), (NULL, 4, 'ab', NULL)) AS t(column1, column2, column3, column4);
824+
----
825+
true true true
826+
true true true
827+
true true true
828+
829+
# a length column above i32::MAX is an error on the array path
830+
query error rpad requested length 2147483648 too large
831+
SELECT rpad('hi', column1) FROM (VALUES (2147483648), (1)) AS t(column1);
832+
766833
# a large scalar target length skips the scalar fast path
767834
query I
768835
SELECT character_length(rpad('x', 16385, 'a'));

0 commit comments

Comments
 (0)