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