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 Power {
20 info: FunctionInfo,
21}
22
23impl Default for Power {
24 fn default() -> Self {
25 Self::new()
26 }
27}
28
29impl Power {
30 pub fn new() -> Self {
31 Self {
32 info: FunctionInfo::new("math::power"),
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
435impl Function for Power {
436 fn info(&self) -> &FunctionInfo {
437 &self.info
438 }
439
440 fn capabilities(&self) -> &[FunctionCapability] {
441 &[FunctionCapability::Scalar]
442 }
443
444 fn return_type(&self, input_types: &[Type]) -> Type {
445 if input_types.len() >= 2 {
446 promote_two(input_types[0].clone(), input_types[1].clone())
447 } else {
448 Type::Float8
449 }
450 }
451
452 fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
453 if args.len() != 2 {
454 return Err(FunctionError::ArityMismatch {
455 function: ctx.fragment.clone(),
456 expected: 2,
457 actual: args.len(),
458 });
459 }
460
461 let base_column = &args[0];
462 let exponent_column = &args[1];
463
464 let (base_data, base_bv) = base_column.data().unwrap_option();
465 let (exp_data, exp_bv) = exponent_column.data().unwrap_option();
466 let row_count = base_data.len();
467
468 let result_data = match (base_data, exp_data) {
469 (ColumnData::Int1(base), ColumnData::Int1(exp)) => {
470 let mut result = Vec::with_capacity(row_count);
471 let mut res_bitvec = Vec::with_capacity(row_count);
472 for i in 0..row_count {
473 match (base.get(i), exp.get(i)) {
474 (Some(&b), Some(&e)) => {
475 result.push(if e < 0 {
476 0
477 } else {
478 (b as i32).pow(e as u32)
479 });
480 res_bitvec.push(true);
481 }
482 _ => {
483 result.push(0);
484 res_bitvec.push(false);
485 }
486 }
487 }
488 ColumnData::int4_with_bitvec(result, res_bitvec)
489 }
490 (ColumnData::Int2(base), ColumnData::Int2(exp)) => {
491 let mut result = Vec::with_capacity(row_count);
492 let mut res_bitvec = Vec::with_capacity(row_count);
493 for i in 0..row_count {
494 match (base.get(i), exp.get(i)) {
495 (Some(&b), Some(&e)) => {
496 result.push(if e < 0 {
497 0
498 } else {
499 (b as i32).pow(e as u32)
500 });
501 res_bitvec.push(true);
502 }
503 _ => {
504 result.push(0);
505 res_bitvec.push(false);
506 }
507 }
508 }
509 ColumnData::int4_with_bitvec(result, res_bitvec)
510 }
511 (ColumnData::Int4(base), ColumnData::Int4(exp)) => {
512 let mut result = Vec::with_capacity(row_count);
513 let mut res_bitvec = Vec::with_capacity(row_count);
514 for i in 0..row_count {
515 match (base.get(i), exp.get(i)) {
516 (Some(&b), Some(&e)) => {
517 result.push(if e < 0 {
518 0
519 } else {
520 b.saturating_pow(e as u32)
521 });
522 res_bitvec.push(true);
523 }
524 _ => {
525 result.push(0);
526 res_bitvec.push(false);
527 }
528 }
529 }
530 ColumnData::int4_with_bitvec(result, res_bitvec)
531 }
532 (ColumnData::Int8(base), ColumnData::Int8(exp)) => {
533 let mut result = Vec::with_capacity(row_count);
534 let mut res_bitvec = Vec::with_capacity(row_count);
535 for i in 0..row_count {
536 match (base.get(i), exp.get(i)) {
537 (Some(&b), Some(&e)) => {
538 result.push(if e < 0 {
539 0
540 } else {
541 b.saturating_pow(e as u32)
542 });
543 res_bitvec.push(true);
544 }
545 _ => {
546 result.push(0);
547 res_bitvec.push(false);
548 }
549 }
550 }
551 ColumnData::int8_with_bitvec(result, res_bitvec)
552 }
553 (ColumnData::Int16(base), ColumnData::Int16(exp)) => {
554 let mut result = Vec::with_capacity(row_count);
555 let mut res_bitvec = Vec::with_capacity(row_count);
556 for i in 0..row_count {
557 match (base.get(i), exp.get(i)) {
558 (Some(&b), Some(&e)) => {
559 result.push(if e < 0 {
560 0
561 } else {
562 b.saturating_pow(e as u32)
563 });
564 res_bitvec.push(true);
565 }
566 _ => {
567 result.push(0);
568 res_bitvec.push(false);
569 }
570 }
571 }
572 ColumnData::int16_with_bitvec(result, res_bitvec)
573 }
574 (ColumnData::Float4(base), ColumnData::Float4(exp)) => {
575 let mut result = Vec::with_capacity(row_count);
576 let mut res_bitvec = Vec::with_capacity(row_count);
577 for i in 0..row_count {
578 match (base.get(i), exp.get(i)) {
579 (Some(&b), Some(&e)) => {
580 result.push(b.powf(e));
581 res_bitvec.push(true);
582 }
583 _ => {
584 result.push(0.0);
585 res_bitvec.push(false);
586 }
587 }
588 }
589 ColumnData::float4_with_bitvec(result, res_bitvec)
590 }
591 (ColumnData::Float8(base), ColumnData::Float8(exp)) => {
592 let mut result = Vec::with_capacity(row_count);
593 let mut res_bitvec = Vec::with_capacity(row_count);
594 for i in 0..row_count {
595 match (base.get(i), exp.get(i)) {
596 (Some(&b), Some(&e)) => {
597 result.push(b.powf(e));
598 res_bitvec.push(true);
599 }
600 _ => {
601 result.push(0.0);
602 res_bitvec.push(false);
603 }
604 }
605 }
606 ColumnData::float8_with_bitvec(result, res_bitvec)
607 }
608 (ColumnData::Uint1(base), ColumnData::Uint1(exp)) => {
609 let mut result = Vec::with_capacity(row_count);
610 let mut res_bitvec = Vec::with_capacity(row_count);
611 for i in 0..row_count {
612 match (base.get(i), exp.get(i)) {
613 (Some(&b), Some(&e)) => {
614 result.push((b as u32).saturating_pow(e as u32));
615 res_bitvec.push(true);
616 }
617 _ => {
618 result.push(0);
619 res_bitvec.push(false);
620 }
621 }
622 }
623 ColumnData::uint4_with_bitvec(result, res_bitvec)
624 }
625 (ColumnData::Uint2(base), ColumnData::Uint2(exp)) => {
626 let mut result = Vec::with_capacity(row_count);
627 let mut res_bitvec = Vec::with_capacity(row_count);
628 for i in 0..row_count {
629 match (base.get(i), exp.get(i)) {
630 (Some(&b), Some(&e)) => {
631 result.push((b as u32).saturating_pow(e as u32));
632 res_bitvec.push(true);
633 }
634 _ => {
635 result.push(0);
636 res_bitvec.push(false);
637 }
638 }
639 }
640 ColumnData::uint4_with_bitvec(result, res_bitvec)
641 }
642 (ColumnData::Uint4(base), ColumnData::Uint4(exp)) => {
643 let mut result = Vec::with_capacity(row_count);
644 let mut res_bitvec = Vec::with_capacity(row_count);
645 for i in 0..row_count {
646 match (base.get(i), exp.get(i)) {
647 (Some(&b), Some(&e)) => {
648 result.push(b.saturating_pow(e));
649 res_bitvec.push(true);
650 }
651 _ => {
652 result.push(0);
653 res_bitvec.push(false);
654 }
655 }
656 }
657 ColumnData::uint4_with_bitvec(result, res_bitvec)
658 }
659 (ColumnData::Uint8(base), ColumnData::Uint8(exp)) => {
660 let mut result = Vec::with_capacity(row_count);
661 let mut res_bitvec = Vec::with_capacity(row_count);
662 for i in 0..row_count {
663 match (base.get(i), exp.get(i)) {
664 (Some(&b), Some(&e)) => {
665 result.push(b.saturating_pow(e as u32));
666 res_bitvec.push(true);
667 }
668 _ => {
669 result.push(0);
670 res_bitvec.push(false);
671 }
672 }
673 }
674 ColumnData::uint8_with_bitvec(result, res_bitvec)
675 }
676 (ColumnData::Uint16(base), ColumnData::Uint16(exp)) => {
677 let mut result = Vec::with_capacity(row_count);
678 let mut res_bitvec = Vec::with_capacity(row_count);
679 for i in 0..row_count {
680 match (base.get(i), exp.get(i)) {
681 (Some(&b), Some(&e)) => {
682 result.push(b.saturating_pow(e as u32));
683 res_bitvec.push(true);
684 }
685 _ => {
686 result.push(0);
687 res_bitvec.push(false);
688 }
689 }
690 }
691 ColumnData::uint16_with_bitvec(result, res_bitvec)
692 }
693 (
694 ColumnData::Int {
695 container: base,
696 max_bytes,
697 },
698 ColumnData::Int {
699 container: exp,
700 ..
701 },
702 ) => {
703 let mut result = Vec::with_capacity(row_count);
704 let mut res_bitvec = Vec::with_capacity(row_count);
705 for i in 0..row_count {
706 match (base.get(i), exp.get(i)) {
707 (Some(b), Some(e)) => {
708 let b_val = b.0.to_f64().unwrap_or(0.0);
709 let e_val = e.0.to_f64().unwrap_or(0.0);
710 result.push(Int::from(b_val.powf(e_val) as i64));
711 res_bitvec.push(true);
712 }
713 _ => {
714 result.push(Int::default());
715 res_bitvec.push(false);
716 }
717 }
718 }
719 ColumnData::Int {
720 container: NumberContainer::new(result),
721 max_bytes: *max_bytes,
722 }
723 }
724 (
725 ColumnData::Uint {
726 container: base,
727 max_bytes,
728 },
729 ColumnData::Uint {
730 container: exp,
731 ..
732 },
733 ) => {
734 let mut result = Vec::with_capacity(row_count);
735 let mut res_bitvec = Vec::with_capacity(row_count);
736 for i in 0..row_count {
737 match (base.get(i), exp.get(i)) {
738 (Some(b), Some(e)) => {
739 let b_val = b.0.to_f64().unwrap_or(0.0);
740 let e_val = e.0.to_f64().unwrap_or(0.0);
741 result.push(Uint::from(b_val.powf(e_val) 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: base,
758 precision,
759 scale,
760 },
761 ColumnData::Decimal {
762 container: exp,
763 ..
764 },
765 ) => {
766 let mut result = Vec::with_capacity(row_count);
767 let mut res_bitvec = Vec::with_capacity(row_count);
768 for i in 0..row_count {
769 match (base.get(i), exp.get(i)) {
770 (Some(b), Some(e)) => {
771 let b_val = b.0.to_f64().unwrap_or(0.0);
772 let e_val = e.0.to_f64().unwrap_or(0.0);
773 result.push(Decimal::from(b_val.powf(e_val)));
774 res_bitvec.push(true);
775 }
776 _ => {
777 result.push(Decimal::default());
778 res_bitvec.push(false);
779 }
780 }
781 }
782 ColumnData::Decimal {
783 container: NumberContainer::new(result),
784 precision: *precision,
785 scale: *scale,
786 }
787 }
788 _ => {
790 let base_type = base_data.get_type();
791 let exp_type = exp_data.get_type();
792
793 if !base_type.is_number() || !exp_type.is_number() {
794 return Err(FunctionError::InvalidArgumentType {
795 function: ctx.fragment.clone(),
796 argument_index: 0,
797 expected: InputTypes::numeric().expected_at(0).to_vec(),
798 actual: base_type,
799 });
800 }
801
802 let promoted_type = promote_two(base_type, exp_type);
803 let promoted_base = convert_column_to_type(base_data, promoted_type.clone(), row_count);
804 let promoted_exp = convert_column_to_type(exp_data, promoted_type, row_count);
805
806 let base_col = Column::new(Fragment::internal("base"), promoted_base);
807 let exp_col = Column::new(Fragment::internal("exp"), promoted_exp);
808 let promoted_columns = Columns::new(vec![base_col, exp_col]);
809
810 return self.call(ctx, &promoted_columns);
811 }
812 };
813
814 let combined_bitvec = match (base_bv, exp_bv) {
815 (Some(b), Some(e)) => Some(b.and(e)),
816 (Some(b), None) => Some(b.clone()),
817 (None, Some(e)) => Some(e.clone()),
818 (None, None) => None,
819 };
820
821 let final_data = if let Some(bv) = combined_bitvec {
822 ColumnData::Option {
823 inner: Box::new(result_data),
824 bitvec: bv,
825 }
826 } else {
827 result_data
828 };
829
830 Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
831 }
832}