risingwave_expr_impl/scalar/
repeat.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("repeat(varchar, int4) -> varchar")]
18pub fn repeat(s: &str, count: i32, writer: &mut impl std::fmt::Write) {
19    for _ in 0..count {
20        writer.write_str(s).unwrap();
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn test_repeat() {
30        let cases = vec![
31            ("hello, world", 1, "hello, world"),
32            ("114514", 3, "114514114514114514"),
33            ("ssss", 0, ""),
34            ("ssss", -114514, ""),
35        ];
36
37        for (s, count, expected) in cases {
38            let mut writer = String::new();
39            repeat(s, count, &mut writer);
40            assert_eq!(writer, expected);
41        }
42    }
43}