1use num_traits::ToPrimitive;
5use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
6use reifydb_type::{
7 fragment::Fragment,
8 value::{
9 container::number::NumberContainer,
10 decimal::Decimal,
11 int::Int,
12 r#type::{Type, input_types::InputTypes},
13 uint::Uint,
14 },
15};
16
17use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
18
19pub struct Clamp {
20 info: FunctionInfo,
21}
22
23impl Default for Clamp {
24 fn default() -> Self {
25 Self::new()
26 }
27}
28
29impl Clamp {
30 pub fn new() -> Self {
31 Self {
32 info: FunctionInfo::new("math::clamp"),
33 }
34 }
35}
36
37fn convert_column_to_type(data: &ColumnData, target: Type, row_count: usize) -> ColumnData {
38 match target {
39 Type::Int1 => {
40 let mut result = Vec::with_capacity(row_count);
41 let mut bitvec = Vec::with_capacity(row_count);
42 for i in 0..row_count {
43 if data.is_defined(i) {
44 result.push(get_as_i8(data, i));
45 bitvec.push(true);
46 } else {
47 result.push(0);
48 bitvec.push(false);
49 }
50 }
51 ColumnData::int1_with_bitvec(result, bitvec)
52 }
53 Type::Int2 => {
54 let mut result = Vec::with_capacity(row_count);
55 let mut bitvec = Vec::with_capacity(row_count);
56 for i in 0..row_count {
57 if data.is_defined(i) {
58 result.push(get_as_i16(data, i));
59 bitvec.push(true);
60 } else {
61 result.push(0);
62 bitvec.push(false);
63 }
64 }
65 ColumnData::int2_with_bitvec(result, bitvec)
66 }
67 Type::Int4 => {
68 let mut result = Vec::with_capacity(row_count);
69 let mut bitvec = Vec::with_capacity(row_count);
70 for i in 0..row_count {
71 if data.is_defined(i) {
72 result.push(get_as_i32(data, i));
73 bitvec.push(true);
74 } else {
75 result.push(0);
76 bitvec.push(false);
77 }
78 }
79 ColumnData::int4_with_bitvec(result, bitvec)
80 }
81 Type::Int8 => {
82 let mut result = Vec::with_capacity(row_count);
83 let mut bitvec = Vec::with_capacity(row_count);
84 for i in 0..row_count {
85 if data.is_defined(i) {
86 result.push(get_as_i64(data, i));
87 bitvec.push(true);
88 } else {
89 result.push(0);
90 bitvec.push(false);
91 }
92 }
93 ColumnData::int8_with_bitvec(result, bitvec)
94 }
95 Type::Int16 => {
96 let mut result = Vec::with_capacity(row_count);
97 let mut bitvec = Vec::with_capacity(row_count);
98 for i in 0..row_count {
99 if data.is_defined(i) {
100 result.push(get_as_i128(data, i));
101 bitvec.push(true);
102 } else {
103 result.push(0);
104 bitvec.push(false);
105 }
106 }
107 ColumnData::int16_with_bitvec(result, bitvec)
108 }
109 Type::Uint1 => {
110 let mut result = Vec::with_capacity(row_count);
111 let mut bitvec = Vec::with_capacity(row_count);
112 for i in 0..row_count {
113 if data.is_defined(i) {
114 result.push(get_as_u8(data, i));
115 bitvec.push(true);
116 } else {
117 result.push(0);
118 bitvec.push(false);
119 }
120 }
121 ColumnData::uint1_with_bitvec(result, bitvec)
122 }
123 Type::Uint2 => {
124 let mut result = Vec::with_capacity(row_count);
125 let mut bitvec = Vec::with_capacity(row_count);
126 for i in 0..row_count {
127 if data.is_defined(i) {
128 result.push(get_as_u16(data, i));
129 bitvec.push(true);
130 } else {
131 result.push(0);
132 bitvec.push(false);
133 }
134 }
135 ColumnData::uint2_with_bitvec(result, bitvec)
136 }
137 Type::Uint4 => {
138 let mut result = Vec::with_capacity(row_count);
139 let mut bitvec = Vec::with_capacity(row_count);
140 for i in 0..row_count {
141 if data.is_defined(i) {
142 result.push(get_as_u32(data, i));
143 bitvec.push(true);
144 } else {
145 result.push(0);
146 bitvec.push(false);
147 }
148 }
149 ColumnData::uint4_with_bitvec(result, bitvec)
150 }
151 Type::Uint8 => {
152 let mut result = Vec::with_capacity(row_count);
153 let mut bitvec = Vec::with_capacity(row_count);
154 for i in 0..row_count {
155 if data.is_defined(i) {
156 result.push(get_as_u64(data, i));
157 bitvec.push(true);
158 } else {
159 result.push(0);
160 bitvec.push(false);
161 }
162 }
163 ColumnData::uint8_with_bitvec(result, bitvec)
164 }
165 Type::Uint16 => {
166 let mut result = Vec::with_capacity(row_count);
167 let mut bitvec = Vec::with_capacity(row_count);
168 for i in 0..row_count {
169 if data.is_defined(i) {
170 result.push(get_as_u128(data, i));
171 bitvec.push(true);
172 } else {
173 result.push(0);
174 bitvec.push(false);
175 }
176 }
177 ColumnData::uint16_with_bitvec(result, bitvec)
178 }
179 Type::Float4 => {
180 let mut result = Vec::with_capacity(row_count);
181 let mut bitvec = Vec::with_capacity(row_count);
182 for i in 0..row_count {
183 if data.is_defined(i) {
184 result.push(get_as_f32(data, i));
185 bitvec.push(true);
186 } else {
187 result.push(0.0);
188 bitvec.push(false);
189 }
190 }
191 ColumnData::float4_with_bitvec(result, bitvec)
192 }
193 Type::Float8 => {
194 let mut result = Vec::with_capacity(row_count);
195 let mut bitvec = Vec::with_capacity(row_count);
196 for i in 0..row_count {
197 if data.is_defined(i) {
198 result.push(get_as_f64(data, i));
199 bitvec.push(true);
200 } else {
201 result.push(0.0);
202 bitvec.push(false);
203 }
204 }
205 ColumnData::float8_with_bitvec(result, bitvec)
206 }
207 Type::Int => {
208 let mut result = Vec::with_capacity(row_count);
209 let mut bitvec = Vec::with_capacity(row_count);
210 for i in 0..row_count {
211 if data.is_defined(i) {
212 result.push(Int::from(get_as_i128(data, i)));
213 bitvec.push(true);
214 } else {
215 result.push(Int::default());
216 bitvec.push(false);
217 }
218 }
219 ColumnData::int_with_bitvec(result, bitvec)
220 }
221 Type::Uint => {
222 let mut result = Vec::with_capacity(row_count);
223 let mut bitvec = Vec::with_capacity(row_count);
224 for i in 0..row_count {
225 if data.is_defined(i) {
226 result.push(Uint::from(get_as_u128(data, i)));
227 bitvec.push(true);
228 } else {
229 result.push(Uint::default());
230 bitvec.push(false);
231 }
232 }
233 ColumnData::uint_with_bitvec(result, bitvec)
234 }
235 Type::Decimal => {
236 let mut result = Vec::with_capacity(row_count);
237 let mut bitvec = Vec::with_capacity(row_count);
238 for i in 0..row_count {
239 if data.is_defined(i) {
240 result.push(Decimal::from(get_as_f64(data, i)));
241 bitvec.push(true);
242 } else {
243 result.push(Decimal::default());
244 bitvec.push(false);
245 }
246 }
247 ColumnData::decimal_with_bitvec(result, bitvec)
248 }
249 _ => data.clone(),
250 }
251}
252
253fn get_as_i8(data: &ColumnData, i: usize) -> i8 {
254 match data {
255 ColumnData::Int1(c) => c.get(i).copied().unwrap_or(0),
256 ColumnData::Uint1(c) => c.get(i).map(|&v| v as i8).unwrap_or(0),
257 _ => 0,
258 }
259}
260
261fn get_as_u8(data: &ColumnData, i: usize) -> u8 {
262 match data {
263 ColumnData::Uint1(c) => c.get(i).copied().unwrap_or(0),
264 ColumnData::Int1(c) => c.get(i).map(|&v| v as u8).unwrap_or(0),
265 _ => 0,
266 }
267}
268
269fn get_as_i16(data: &ColumnData, i: usize) -> i16 {
270 match data {
271 ColumnData::Int1(c) => c.get(i).map(|&v| v as i16).unwrap_or(0),
272 ColumnData::Int2(c) => c.get(i).copied().unwrap_or(0),
273 ColumnData::Uint1(c) => c.get(i).map(|&v| v as i16).unwrap_or(0),
274 _ => 0,
275 }
276}
277
278fn get_as_i32(data: &ColumnData, i: usize) -> i32 {
279 match data {
280 ColumnData::Int1(c) => c.get(i).map(|&v| v as i32).unwrap_or(0),
281 ColumnData::Int2(c) => c.get(i).map(|&v| v as i32).unwrap_or(0),
282 ColumnData::Int4(c) => c.get(i).copied().unwrap_or(0),
283 ColumnData::Uint1(c) => c.get(i).map(|&v| v as i32).unwrap_or(0),
284 ColumnData::Uint2(c) => c.get(i).map(|&v| v as i32).unwrap_or(0),
285 _ => 0,
286 }
287}
288
289fn get_as_i64(data: &ColumnData, i: usize) -> i64 {
290 match data {
291 ColumnData::Int1(c) => c.get(i).map(|&v| v as i64).unwrap_or(0),
292 ColumnData::Int2(c) => c.get(i).map(|&v| v as i64).unwrap_or(0),
293 ColumnData::Int4(c) => c.get(i).map(|&v| v as i64).unwrap_or(0),
294 ColumnData::Int8(c) => c.get(i).copied().unwrap_or(0),
295 ColumnData::Uint1(c) => c.get(i).map(|&v| v as i64).unwrap_or(0),
296 ColumnData::Uint2(c) => c.get(i).map(|&v| v as i64).unwrap_or(0),
297 ColumnData::Uint4(c) => c.get(i).map(|&v| v as i64).unwrap_or(0),
298 _ => 0,
299 }
300}
301
302fn get_as_i128(data: &ColumnData, i: usize) -> i128 {
303 match data {
304 ColumnData::Int1(c) => c.get(i).map(|&v| v as i128).unwrap_or(0),
305 ColumnData::Int2(c) => c.get(i).map(|&v| v as i128).unwrap_or(0),
306 ColumnData::Int4(c) => c.get(i).map(|&v| v as i128).unwrap_or(0),
307 ColumnData::Int8(c) => c.get(i).map(|&v| v as i128).unwrap_or(0),
308 ColumnData::Int16(c) => c.get(i).copied().unwrap_or(0),
309 ColumnData::Uint1(c) => c.get(i).map(|&v| v as i128).unwrap_or(0),
310 ColumnData::Uint2(c) => c.get(i).map(|&v| v as i128).unwrap_or(0),
311 ColumnData::Uint4(c) => c.get(i).map(|&v| v as i128).unwrap_or(0),
312 ColumnData::Uint8(c) => c.get(i).map(|&v| v as i128).unwrap_or(0),
313 _ => 0,
314 }
315}
316
317fn get_as_u16(data: &ColumnData, i: usize) -> u16 {
318 match data {
319 ColumnData::Uint1(c) => c.get(i).map(|&v| v as u16).unwrap_or(0),
320 ColumnData::Uint2(c) => c.get(i).copied().unwrap_or(0),
321 _ => 0,
322 }
323}
324
325fn get_as_u32(data: &ColumnData, i: usize) -> u32 {
326 match data {
327 ColumnData::Uint1(c) => c.get(i).map(|&v| v as u32).unwrap_or(0),
328 ColumnData::Uint2(c) => c.get(i).map(|&v| v as u32).unwrap_or(0),
329 ColumnData::Uint4(c) => c.get(i).copied().unwrap_or(0),
330 _ => 0,
331 }
332}
333
334fn get_as_u64(data: &ColumnData, i: usize) -> u64 {
335 match data {
336 ColumnData::Uint1(c) => c.get(i).map(|&v| v as u64).unwrap_or(0),
337 ColumnData::Uint2(c) => c.get(i).map(|&v| v as u64).unwrap_or(0),
338 ColumnData::Uint4(c) => c.get(i).map(|&v| v as u64).unwrap_or(0),
339 ColumnData::Uint8(c) => c.get(i).copied().unwrap_or(0),
340 _ => 0,
341 }
342}
343
344fn get_as_u128(data: &ColumnData, i: usize) -> u128 {
345 match data {
346 ColumnData::Uint1(c) => c.get(i).map(|&v| v as u128).unwrap_or(0),
347 ColumnData::Uint2(c) => c.get(i).map(|&v| v as u128).unwrap_or(0),
348 ColumnData::Uint4(c) => c.get(i).map(|&v| v as u128).unwrap_or(0),
349 ColumnData::Uint8(c) => c.get(i).map(|&v| v as u128).unwrap_or(0),
350 ColumnData::Uint16(c) => c.get(i).copied().unwrap_or(0),
351 _ => 0,
352 }
353}
354
355fn get_as_f32(data: &ColumnData, i: usize) -> f32 {
356 match data {
357 ColumnData::Int1(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
358 ColumnData::Int2(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
359 ColumnData::Int4(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
360 ColumnData::Int8(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
361 ColumnData::Int16(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
362 ColumnData::Uint1(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
363 ColumnData::Uint2(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
364 ColumnData::Uint4(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
365 ColumnData::Uint8(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
366 ColumnData::Uint16(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
367 ColumnData::Float4(c) => c.get(i).copied().unwrap_or(0.0),
368 ColumnData::Float8(c) => c.get(i).map(|&v| v as f32).unwrap_or(0.0),
369 ColumnData::Int {
370 container,
371 ..
372 } => container.get(i).map(|v| v.0.to_f32().unwrap_or(0.0)).unwrap_or(0.0),
373 ColumnData::Uint {
374 container,
375 ..
376 } => container.get(i).map(|v| v.0.to_f32().unwrap_or(0.0)).unwrap_or(0.0),
377 ColumnData::Decimal {
378 container,
379 ..
380 } => container.get(i).map(|v| v.0.to_f32().unwrap_or(0.0)).unwrap_or(0.0),
381 _ => 0.0,
382 }
383}
384
385fn get_as_f64(data: &ColumnData, i: usize) -> f64 {
386 match data {
387 ColumnData::Int1(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
388 ColumnData::Int2(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
389 ColumnData::Int4(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
390 ColumnData::Int8(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
391 ColumnData::Int16(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
392 ColumnData::Uint1(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
393 ColumnData::Uint2(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
394 ColumnData::Uint4(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
395 ColumnData::Uint8(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
396 ColumnData::Uint16(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
397 ColumnData::Float4(c) => c.get(i).map(|&v| v as f64).unwrap_or(0.0),
398 ColumnData::Float8(c) => c.get(i).copied().unwrap_or(0.0),
399 ColumnData::Int {
400 container,
401 ..
402 } => container.get(i).map(|v| v.0.to_f64().unwrap_or(0.0)).unwrap_or(0.0),
403 ColumnData::Uint {
404 container,
405 ..
406 } => container.get(i).map(|v| v.0.to_f64().unwrap_or(0.0)).unwrap_or(0.0),
407 ColumnData::Decimal {
408 container,
409 ..
410 } => container.get(i).map(|v| v.0.to_f64().unwrap_or(0.0)).unwrap_or(0.0),
411 _ => 0.0,
412 }
413}
414
415fn promote_two(left: Type, right: Type) -> Type {
416 if matches!(left, Type::Float4 | Type::Float8 | Type::Decimal)
417 || matches!(right, Type::Float4 | Type::Float8 | Type::Decimal)
418 {
419 return Type::Decimal;
420 }
421 if left == Type::Int || right == Type::Int {
422 return Type::Int16;
423 }
424 if left == Type::Uint || right == Type::Uint {
425 if matches!(left, Type::Int1 | Type::Int2 | Type::Int4 | Type::Int8 | Type::Int16)
426 || matches!(right, Type::Int1 | Type::Int2 | Type::Int4 | Type::Int8 | Type::Int16)
427 {
428 return Type::Int16;
429 }
430 return Type::Uint16;
431 }
432 Type::promote(left, right)
433}
434
435fn promote_numeric_types(a: Type, b: Type, c: Type) -> Type {
436 promote_two(promote_two(a, b), c)
437}
438
439impl Function for Clamp {
440 fn info(&self) -> &FunctionInfo {
441 &self.info
442 }
443
444 fn capabilities(&self) -> &[FunctionCapability] {
445 &[FunctionCapability::Scalar]
446 }
447
448 fn return_type(&self, input_types: &[Type]) -> Type {
449 if input_types.len() >= 3
450 && input_types[0].is_number()
451 && input_types[1].is_number()
452 && input_types[2].is_number()
453 {
454 promote_numeric_types(input_types[0].clone(), input_types[1].clone(), input_types[2].clone())
455 } else {
456 Type::Float8
457 }
458 }
459
460 fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
461 if args.len() != 3 {
462 return Err(FunctionError::ArityMismatch {
463 function: ctx.fragment.clone(),
464 expected: 3,
465 actual: args.len(),
466 });
467 }
468
469 let val_col = &args[0];
470 let min_col = &args[1];
471 let max_col = &args[2];
472
473 let (v_data, v_bv) = val_col.data().unwrap_option();
474 let (lo_data, lo_bv) = min_col.data().unwrap_option();
475 let (hi_data, hi_bv) = max_col.data().unwrap_option();
476 let row_count = v_data.len();
477
478 let result_data = match (v_data, lo_data, hi_data) {
479 (ColumnData::Int1(v), ColumnData::Int1(lo), ColumnData::Int1(hi)) => {
480 let mut result = Vec::with_capacity(row_count);
481 let mut res_bitvec = Vec::with_capacity(row_count);
482 for i in 0..row_count {
483 match (v.get(i), lo.get(i), hi.get(i)) {
484 (Some(&val), Some(&min), Some(&max)) => {
485 result.push(val.clamp(min, max));
486 res_bitvec.push(true);
487 }
488 _ => {
489 result.push(0);
490 res_bitvec.push(false);
491 }
492 }
493 }
494 ColumnData::int1_with_bitvec(result, res_bitvec)
495 }
496 (ColumnData::Int2(v), ColumnData::Int2(lo), ColumnData::Int2(hi)) => {
497 let mut result = Vec::with_capacity(row_count);
498 let mut res_bitvec = Vec::with_capacity(row_count);
499 for i in 0..row_count {
500 match (v.get(i), lo.get(i), hi.get(i)) {
501 (Some(&val), Some(&min), Some(&max)) => {
502 result.push(val.clamp(min, max));
503 res_bitvec.push(true);
504 }
505 _ => {
506 result.push(0);
507 res_bitvec.push(false);
508 }
509 }
510 }
511 ColumnData::int2_with_bitvec(result, res_bitvec)
512 }
513 (ColumnData::Int4(v), ColumnData::Int4(lo), ColumnData::Int4(hi)) => {
514 let mut result = Vec::with_capacity(row_count);
515 let mut res_bitvec = Vec::with_capacity(row_count);
516 for i in 0..row_count {
517 match (v.get(i), lo.get(i), hi.get(i)) {
518 (Some(&val), Some(&min), Some(&max)) => {
519 result.push(val.clamp(min, max));
520 res_bitvec.push(true);
521 }
522 _ => {
523 result.push(0);
524 res_bitvec.push(false);
525 }
526 }
527 }
528 ColumnData::int4_with_bitvec(result, res_bitvec)
529 }
530 (ColumnData::Int8(v), ColumnData::Int8(lo), ColumnData::Int8(hi)) => {
531 let mut result = Vec::with_capacity(row_count);
532 let mut res_bitvec = Vec::with_capacity(row_count);
533 for i in 0..row_count {
534 match (v.get(i), lo.get(i), hi.get(i)) {
535 (Some(&val), Some(&min), Some(&max)) => {
536 result.push(val.clamp(min, max));
537 res_bitvec.push(true);
538 }
539 _ => {
540 result.push(0);
541 res_bitvec.push(false);
542 }
543 }
544 }
545 ColumnData::int8_with_bitvec(result, res_bitvec)
546 }
547 (ColumnData::Int16(v), ColumnData::Int16(lo), ColumnData::Int16(hi)) => {
548 let mut result = Vec::with_capacity(row_count);
549 let mut res_bitvec = Vec::with_capacity(row_count);
550 for i in 0..row_count {
551 match (v.get(i), lo.get(i), hi.get(i)) {
552 (Some(&val), Some(&min), Some(&max)) => {
553 result.push(val.clamp(min, max));
554 res_bitvec.push(true);
555 }
556 _ => {
557 result.push(0);
558 res_bitvec.push(false);
559 }
560 }
561 }
562 ColumnData::int16_with_bitvec(result, res_bitvec)
563 }
564 (ColumnData::Uint1(v), ColumnData::Uint1(lo), ColumnData::Uint1(hi)) => {
565 let mut result = Vec::with_capacity(row_count);
566 let mut res_bitvec = Vec::with_capacity(row_count);
567 for i in 0..row_count {
568 match (v.get(i), lo.get(i), hi.get(i)) {
569 (Some(&val), Some(&min), Some(&max)) => {
570 result.push(val.clamp(min, max));
571 res_bitvec.push(true);
572 }
573 _ => {
574 result.push(0);
575 res_bitvec.push(false);
576 }
577 }
578 }
579 ColumnData::uint1_with_bitvec(result, res_bitvec)
580 }
581 (ColumnData::Uint2(v), ColumnData::Uint2(lo), ColumnData::Uint2(hi)) => {
582 let mut result = Vec::with_capacity(row_count);
583 let mut res_bitvec = Vec::with_capacity(row_count);
584 for i in 0..row_count {
585 match (v.get(i), lo.get(i), hi.get(i)) {
586 (Some(&val), Some(&min), Some(&max)) => {
587 result.push(val.clamp(min, max));
588 res_bitvec.push(true);
589 }
590 _ => {
591 result.push(0);
592 res_bitvec.push(false);
593 }
594 }
595 }
596 ColumnData::uint2_with_bitvec(result, res_bitvec)
597 }
598 (ColumnData::Uint4(v), ColumnData::Uint4(lo), ColumnData::Uint4(hi)) => {
599 let mut result = Vec::with_capacity(row_count);
600 let mut res_bitvec = Vec::with_capacity(row_count);
601 for i in 0..row_count {
602 match (v.get(i), lo.get(i), hi.get(i)) {
603 (Some(&val), Some(&min), Some(&max)) => {
604 result.push(val.clamp(min, max));
605 res_bitvec.push(true);
606 }
607 _ => {
608 result.push(0);
609 res_bitvec.push(false);
610 }
611 }
612 }
613 ColumnData::uint4_with_bitvec(result, res_bitvec)
614 }
615 (ColumnData::Uint8(v), ColumnData::Uint8(lo), ColumnData::Uint8(hi)) => {
616 let mut result = Vec::with_capacity(row_count);
617 let mut res_bitvec = Vec::with_capacity(row_count);
618 for i in 0..row_count {
619 match (v.get(i), lo.get(i), hi.get(i)) {
620 (Some(&val), Some(&min), Some(&max)) => {
621 result.push(val.clamp(min, max));
622 res_bitvec.push(true);
623 }
624 _ => {
625 result.push(0);
626 res_bitvec.push(false);
627 }
628 }
629 }
630 ColumnData::uint8_with_bitvec(result, res_bitvec)
631 }
632 (ColumnData::Uint16(v), ColumnData::Uint16(lo), ColumnData::Uint16(hi)) => {
633 let mut result = Vec::with_capacity(row_count);
634 let mut res_bitvec = Vec::with_capacity(row_count);
635 for i in 0..row_count {
636 match (v.get(i), lo.get(i), hi.get(i)) {
637 (Some(&val), Some(&min), Some(&max)) => {
638 result.push(val.clamp(min, max));
639 res_bitvec.push(true);
640 }
641 _ => {
642 result.push(0);
643 res_bitvec.push(false);
644 }
645 }
646 }
647 ColumnData::uint16_with_bitvec(result, res_bitvec)
648 }
649 (ColumnData::Float4(v), ColumnData::Float4(lo), ColumnData::Float4(hi)) => {
650 let mut result = Vec::with_capacity(row_count);
651 let mut res_bitvec = Vec::with_capacity(row_count);
652 for i in 0..row_count {
653 match (v.get(i), lo.get(i), hi.get(i)) {
654 (Some(&val), Some(&min), Some(&max)) => {
655 result.push(val.clamp(min, max));
656 res_bitvec.push(true);
657 }
658 _ => {
659 result.push(0.0);
660 res_bitvec.push(false);
661 }
662 }
663 }
664 ColumnData::float4_with_bitvec(result, res_bitvec)
665 }
666 (ColumnData::Float8(v), ColumnData::Float8(lo), ColumnData::Float8(hi)) => {
667 let mut result = Vec::with_capacity(row_count);
668 let mut res_bitvec = Vec::with_capacity(row_count);
669 for i in 0..row_count {
670 match (v.get(i), lo.get(i), hi.get(i)) {
671 (Some(&val), Some(&min), Some(&max)) => {
672 result.push(val.clamp(min, max));
673 res_bitvec.push(true);
674 }
675 _ => {
676 result.push(0.0);
677 res_bitvec.push(false);
678 }
679 }
680 }
681 ColumnData::float8_with_bitvec(result, res_bitvec)
682 }
683 (
684 ColumnData::Int {
685 container: v_container,
686 max_bytes,
687 },
688 ColumnData::Int {
689 container: lo_container,
690 ..
691 },
692 ColumnData::Int {
693 container: hi_container,
694 ..
695 },
696 ) => {
697 let mut result = Vec::with_capacity(row_count);
698 let mut res_bitvec = Vec::with_capacity(row_count);
699 for i in 0..row_count {
700 match (v_container.get(i), lo_container.get(i), hi_container.get(i)) {
701 (Some(val), Some(lo), Some(hi)) => {
702 let v = val.0.to_f64().unwrap_or(0.0);
703 let l = lo.0.to_f64().unwrap_or(0.0);
704 let h = hi.0.to_f64().unwrap_or(0.0);
705 result.push(Int::from(v.clamp(l, h) as i64));
706 res_bitvec.push(true);
707 }
708 _ => {
709 result.push(Int::default());
710 res_bitvec.push(false);
711 }
712 }
713 }
714 ColumnData::Int {
715 container: NumberContainer::new(result),
716 max_bytes: *max_bytes,
717 }
718 }
719 (
720 ColumnData::Uint {
721 container: v_container,
722 max_bytes,
723 },
724 ColumnData::Uint {
725 container: lo_container,
726 ..
727 },
728 ColumnData::Uint {
729 container: hi_container,
730 ..
731 },
732 ) => {
733 let mut result = Vec::with_capacity(row_count);
734 let mut res_bitvec = Vec::with_capacity(row_count);
735 for i in 0..row_count {
736 match (v_container.get(i), lo_container.get(i), hi_container.get(i)) {
737 (Some(val), Some(lo), Some(hi)) => {
738 let v = val.0.to_f64().unwrap_or(0.0);
739 let l = lo.0.to_f64().unwrap_or(0.0);
740 let h = hi.0.to_f64().unwrap_or(0.0);
741 result.push(Uint::from(v.clamp(l, h) as u64));
742 res_bitvec.push(true);
743 }
744 _ => {
745 result.push(Uint::default());
746 res_bitvec.push(false);
747 }
748 }
749 }
750 ColumnData::Uint {
751 container: NumberContainer::new(result),
752 max_bytes: *max_bytes,
753 }
754 }
755 (
756 ColumnData::Decimal {
757 container: v_container,
758 precision,
759 scale,
760 },
761 ColumnData::Decimal {
762 container: lo_container,
763 ..
764 },
765 ColumnData::Decimal {
766 container: hi_container,
767 ..
768 },
769 ) => {
770 let mut result = Vec::with_capacity(row_count);
771 let mut res_bitvec = Vec::with_capacity(row_count);
772 for i in 0..row_count {
773 match (v_container.get(i), lo_container.get(i), hi_container.get(i)) {
774 (Some(val), Some(lo), Some(hi)) => {
775 let v = val.0.to_f64().unwrap_or(0.0);
776 let l = lo.0.to_f64().unwrap_or(0.0);
777 let h = hi.0.to_f64().unwrap_or(0.0);
778 result.push(Decimal::from(v.clamp(l, h)));
779 res_bitvec.push(true);
780 }
781 _ => {
782 result.push(Decimal::default());
783 res_bitvec.push(false);
784 }
785 }
786 }
787 ColumnData::Decimal {
788 container: NumberContainer::new(result),
789 precision: *precision,
790 scale: *scale,
791 }
792 }
793 _ => {
795 let v_type = v_data.get_type();
796 let lo_type = lo_data.get_type();
797 let hi_type = hi_data.get_type();
798
799 if !v_type.is_number() {
800 return Err(FunctionError::InvalidArgumentType {
801 function: ctx.fragment.clone(),
802 argument_index: 0,
803 expected: InputTypes::numeric().expected_at(0).to_vec(),
804 actual: v_type,
805 });
806 }
807 if !lo_type.is_number() {
808 return Err(FunctionError::InvalidArgumentType {
809 function: ctx.fragment.clone(),
810 argument_index: 1,
811 expected: InputTypes::numeric().expected_at(0).to_vec(),
812 actual: lo_type,
813 });
814 }
815 if !hi_type.is_number() {
816 return Err(FunctionError::InvalidArgumentType {
817 function: ctx.fragment.clone(),
818 argument_index: 2,
819 expected: InputTypes::numeric().expected_at(0).to_vec(),
820 actual: hi_type,
821 });
822 }
823
824 let promoted = promote_numeric_types(v_type, lo_type, hi_type);
825 let pv = convert_column_to_type(v_data, promoted.clone(), row_count);
826 let plo = convert_column_to_type(lo_data, promoted.clone(), row_count);
827 let phi = convert_column_to_type(hi_data, promoted, row_count);
828
829 let val_col = Column::new(Fragment::internal("val"), pv);
830 let min_col = Column::new(Fragment::internal("min"), plo);
831 let max_col = Column::new(Fragment::internal("max"), phi);
832 let promoted_columns = Columns::new(vec![val_col, min_col, max_col]);
833
834 return self.call(ctx, &promoted_columns);
835 }
836 };
837
838 let combined_bitvec = match (v_bv, lo_bv, hi_bv) {
839 (Some(v), Some(lo), Some(hi)) => Some(v.and(lo).and(hi)),
840 (Some(v), Some(lo), None) => Some(v.and(lo)),
841 (Some(v), None, Some(hi)) => Some(v.and(hi)),
842 (None, Some(lo), Some(hi)) => Some(lo.and(hi)),
843 (Some(v), None, None) => Some(v.clone()),
844 (None, Some(lo), None) => Some(lo.clone()),
845 (None, None, Some(hi)) => Some(hi.clone()),
846 (None, None, None) => None,
847 };
848
849 let final_data = if let Some(bv) = combined_bitvec {
850 ColumnData::Option {
851 inner: Box::new(result_data),
852 bitvec: bv,
853 }
854 } else {
855 result_data
856 };
857
858 Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
859 }
860}