Skip to content

Apache DataFusion SQL Query Engine

MINOR: fix concat_ws corner bug (#2128)

DuRipengbed81eade12b
diff --git a/datafusion/core/tests/sql/expr.rs b/datafusion/core/tests/sql/expr.rs
index ae9ce4be91..086ead0d41 100644
--- a/datafusion/core/tests/sql/expr.rs
+++ b/datafusion/core/tests/sql/expr.rs
@@ -554,6 +554,8 @@ async fn test_string_expressions() -> Result<()> {
test_expression!("concat_ws('|','a','b','c')", "a|b|c");
test_expression!("concat_ws('|',NULL)", "");
test_expression!("concat_ws(NULL,'a',NULL,'b','c')", "NULL");
+ test_expression!("concat_ws('|','a',NULL)", "a");
+ test_expression!("concat_ws('|','a',NULL,NULL)", "a");
test_expression!("initcap('')", "");
test_expression!("initcap('hi THOMAS')", "Hi Thomas");
test_expression!("initcap(NULL)", "NULL");
diff --git a/datafusion/physical-expr/src/string_expressions.rs b/datafusion/physical-expr/src/string_expressions.rs
index b0b569d99e..1f369cdad3 100644
--- a/datafusion/physical-expr/src/string_expressions.rs
+++ b/datafusion/physical-expr/src/string_expressions.rs
@@ -343,18 +343,17 @@ pub fn concat_ws(args: &[ArrayRef]) -> Result<ArrayRef> {
.enumerate()
.map(|(index, x)| {
x.map(|sep: &str| {
- let mut owned_string: String = "".to_owned();
- for arg_index in 1..args.len() {
- let arg = &args[arg_index];
- if !arg.is_null(index) {
- owned_string.push_str(arg.value(index));
- // if not last push separator
- if arg_index != args.len() - 1 {
- owned_string.push_str(sep);
+ let string_vec = args[1..]
+ .iter()
+ .flat_map(|arg| {
+ if !arg.is_null(index) {
+ Some(arg.value(index))
+ } else {
+ None
}
- }
- }
- owned_string
+ })
+ .collect::<Vec<&str>>();
+ string_vec.join(sep)
})
})
.collect::<StringArray>();