risingwave_expr_impl/scalar/
concat_op.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_expr::function;
16
17#[function("concat_op(varchar, varchar) -> varchar")]
18pub fn concat_op(left: &str, right: &str, writer: &mut impl std::fmt::Write) {
19    writer.write_str(left).unwrap();
20    writer.write_str(right).unwrap();
21}
22
23/// Concatenates the two binary strings.
24///
25/// # Example
26///
27/// ```slt
28/// query I
29/// select '\x123456'::bytea || '\x789a00bcde'::bytea;
30/// ----
31/// \x123456789a00bcde
32///
33/// query I
34/// select '\x123456'::bytea || '\x789a00bcde';
35/// ----
36/// \x123456789a00bcde
37///
38/// query I
39/// select '\x123456'::bytea || ''::bytea;
40/// ----
41/// \x123456
42/// ```
43#[function("bytea_concat_op(bytea, bytea) -> bytea")]
44pub fn bytea_concat_op(left: &[u8], right: &[u8], writer: &mut impl std::io::Write) {
45    writer.write_all(left).unwrap();
46    writer.write_all(right).unwrap();
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_concat_op() {
55        let mut s = String::new();
56        concat_op("114", "514", &mut s);
57        assert_eq!(s, "114514")
58    }
59
60    #[test]
61    fn test_bytea_concat_op() {
62        let left = b"\x01\x02\x03";
63        let right = b"\x04\x05";
64        let mut result = Vec::new();
65        bytea_concat_op(left, right, &mut result);
66        assert_eq!(&result, b"\x01\x02\x03\x04\x05");
67    }
68}