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