risingwave_expr_impl/scalar/
concat.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use risingwave_common::row::Row;
16use risingwave_common::types::ToText;
17use risingwave_expr::function;
18
19/// Concatenates the text representations of all the arguments. NULL arguments are ignored.
20///
21/// # Example
22///
23/// ```slt
24/// query T
25/// select concat('abcde', 2, NULL, 22);
26/// ----
27/// abcde222
28///
29/// query T
30/// select concat(variadic array['abcde', '2', NULL, '22']);
31/// ----
32/// abcde222
33/// ```
34#[function("concat(variadic anyarray) -> varchar")]
35fn concat(vals: impl Row, writer: &mut impl std::fmt::Write) {
36    for string in vals.iter().flatten() {
37        string.write(writer).unwrap();
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use risingwave_common::array::DataChunk;
44    use risingwave_common::row::Row;
45    use risingwave_common::test_prelude::DataChunkTestExt;
46    use risingwave_common::types::ToOwnedDatum;
47    use risingwave_common::util::iter_util::ZipEqDebug;
48    use risingwave_expr::expr::build_from_pretty;
49
50    #[tokio::test]
51    async fn test_concat() {
52        let concat = build_from_pretty("(concat:varchar $0:varchar $1:varchar $2:varchar)");
53        let (input, expected) = DataChunk::from_pretty(
54            "T T T  T
55             a b c  abc
56             . b c  bc
57             . . .  (empty)",
58        )
59        .split_column_at(3);
60
61        // test eval
62        let output = concat.eval(&input).await.unwrap();
63        assert_eq!(&output, expected.column_at(0));
64
65        // test eval_row
66        for (row, expected) in input.rows().zip_eq_debug(expected.rows()) {
67            let result = concat.eval_row(&row.to_owned_row()).await.unwrap();
68            assert_eq!(result, expected.datum_at(0).to_owned_datum());
69        }
70    }
71}