reifydb_function/date/
end_of_month.rs1use reifydb_core::value::column::data::ColumnData;
5use reifydb_type::value::{container::temporal::TemporalContainer, date::Date, r#type::Type};
6
7use crate::{ScalarFunction, ScalarFunctionContext, error::ScalarFunctionError, propagate_options};
8
9pub struct DateEndOfMonth;
10
11impl DateEndOfMonth {
12 pub fn new() -> Self {
13 Self
14 }
15}
16
17impl ScalarFunction for DateEndOfMonth {
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 result = TemporalContainer::with_capacity(row_count);
39
40 for i in 0..row_count {
41 if let Some(date) = container.get(i) {
42 let year = date.year();
43 let month = date.month();
44 let last_day = Date::days_in_month(year, month);
45 match Date::new(year, month, last_day) {
46 Some(d) => result.push(d),
47 None => result.push_default(),
48 }
49 } else {
50 result.push_default();
51 }
52 }
53
54 Ok(ColumnData::Date(result))
55 }
56 other => Err(ScalarFunctionError::InvalidArgumentType {
57 function: ctx.fragment.clone(),
58 argument_index: 0,
59 expected: vec![Type::Date],
60 actual: other.get_type(),
61 }),
62 }
63 }
64
65 fn return_type(&self, _input_types: &[Type]) -> Type {
66 Type::Date
67 }
68}