reifydb_function/date/
is_leap_year.rs1use reifydb_core::value::column::data::ColumnData;
5use reifydb_type::value::{date::Date, r#type::Type};
6
7use crate::{ScalarFunction, ScalarFunctionContext, error::ScalarFunctionError, propagate_options};
8
9pub struct DateIsLeapYear;
10
11impl DateIsLeapYear {
12 pub fn new() -> Self {
13 Self
14 }
15}
16
17impl ScalarFunction for DateIsLeapYear {
18 fn scalar(&self, ctx: ScalarFunctionContext) -> crate::error::ScalarFunctionResult<ColumnData> {
19 if let Some(result) = propagate_options(self, &ctx) {
20 return result;
21 }
22
23 let columns = ctx.columns;
24 let row_count = ctx.row_count;
25
26 if columns.len() != 1 {
27 return Err(ScalarFunctionError::ArityMismatch {
28 function: ctx.fragment.clone(),
29 expected: 1,
30 actual: columns.len(),
31 });
32 }
33
34 let col = columns.get(0).unwrap();
35
36 match col.data() {
37 ColumnData::Date(container) => {
38 let mut data = Vec::with_capacity(row_count);
39 let mut bitvec = Vec::with_capacity(row_count);
40
41 for i in 0..row_count {
42 if let Some(date) = container.get(i) {
43 data.push(Date::is_leap_year(date.year()));
44 bitvec.push(true);
45 } else {
46 data.push(false);
47 bitvec.push(false);
48 }
49 }
50
51 Ok(ColumnData::bool_with_bitvec(data, bitvec))
52 }
53 other => Err(ScalarFunctionError::InvalidArgumentType {
54 function: ctx.fragment.clone(),
55 argument_index: 0,
56 expected: vec![Type::Date],
57 actual: other.get_type(),
58 }),
59 }
60 }
61
62 fn return_type(&self, _input_types: &[Type]) -> Type {
63 Type::Boolean
64 }
65}