reifydb_routine/function/text/
repeat.rs1use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
5use reifydb_type::value::{constraint::bytes::MaxBytes, container::utf8::Utf8Container, r#type::Type};
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct TextRepeat {
10 info: FunctionInfo,
11}
12
13impl Default for TextRepeat {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl TextRepeat {
20 pub fn new() -> Self {
21 Self {
22 info: FunctionInfo::new("text::repeat"),
23 }
24 }
25}
26
27impl Function for TextRepeat {
28 fn info(&self) -> &FunctionInfo {
29 &self.info
30 }
31
32 fn capabilities(&self) -> &[FunctionCapability] {
33 &[FunctionCapability::Scalar]
34 }
35
36 fn return_type(&self, _input_types: &[Type]) -> Type {
37 Type::Utf8
38 }
39
40 fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
41 if args.len() != 2 {
42 return Err(FunctionError::ArityMismatch {
43 function: ctx.fragment.clone(),
44 expected: 2,
45 actual: args.len(),
46 });
47 }
48
49 let str_col = &args[0];
50 let count_col = &args[1];
51
52 let (str_data, str_bv) = str_col.data().unwrap_option();
53 let (count_data, count_bv) = count_col.data().unwrap_option();
54 let row_count = str_data.len();
55
56 match str_data {
57 ColumnData::Utf8 {
58 container: str_container,
59 ..
60 } => {
61 let mut result_data = Vec::with_capacity(row_count);
62
63 for i in 0..row_count {
64 if !str_container.is_defined(i) {
65 result_data.push(String::new());
66 continue;
67 }
68
69 let count = match count_data {
70 ColumnData::Int1(c) => c.get(i).map(|&v| v as i64),
71 ColumnData::Int2(c) => c.get(i).map(|&v| v as i64),
72 ColumnData::Int4(c) => c.get(i).map(|&v| v as i64),
73 ColumnData::Int8(c) => c.get(i).copied(),
74 ColumnData::Uint1(c) => c.get(i).map(|&v| v as i64),
75 ColumnData::Uint2(c) => c.get(i).map(|&v| v as i64),
76 ColumnData::Uint4(c) => c.get(i).map(|&v| v as i64),
77 _ => {
78 return Err(FunctionError::InvalidArgumentType {
79 function: ctx.fragment.clone(),
80 argument_index: 1,
81 expected: vec![
82 Type::Int1,
83 Type::Int2,
84 Type::Int4,
85 Type::Int8,
86 ],
87 actual: count_data.get_type(),
88 });
89 }
90 };
91
92 match count {
93 Some(n) if n >= 0 => {
94 let s = &str_container[i];
95 result_data.push(s.repeat(n as usize));
96 }
97 Some(_) => {
98 result_data.push(String::new());
99 }
100 None => {
101 result_data.push(String::new());
102 }
103 }
104 }
105
106 let result_col_data = ColumnData::Utf8 {
107 container: Utf8Container::new(result_data),
108 max_bytes: MaxBytes::MAX,
109 };
110
111 let combined_bv = match (str_bv, count_bv) {
112 (Some(b), Some(e)) => Some(b.and(e)),
113 (Some(b), None) => Some(b.clone()),
114 (None, Some(e)) => Some(e.clone()),
115 (None, None) => None,
116 };
117
118 let final_data = match combined_bv {
119 Some(bv) => ColumnData::Option {
120 inner: Box::new(result_col_data),
121 bitvec: bv,
122 },
123 None => result_col_data,
124 };
125 Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
126 }
127 other => Err(FunctionError::InvalidArgumentType {
128 function: ctx.fragment.clone(),
129 argument_index: 0,
130 expected: vec![Type::Utf8],
131 actual: other.get_type(),
132 }),
133 }
134 }
135}