reifydb_routine/function/clock/
now.rs1use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
5use reifydb_type::value::r#type::Type;
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct Now {
10 info: FunctionInfo,
11}
12
13impl Default for Now {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl Now {
20 pub fn new() -> Self {
21 Self {
22 info: FunctionInfo::new("clock::now"),
23 }
24 }
25}
26
27impl Function for Now {
28 fn info(&self) -> &FunctionInfo {
29 &self.info
30 }
31
32 fn capabilities(&self) -> &[FunctionCapability] {
33 &[FunctionCapability::Scalar]
34 }
35
36 fn return_type(&self, _input_types: &[Type]) -> Type {
37 Type::Int8
38 }
39
40 fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
41 if !args.is_empty() {
42 return Err(FunctionError::ArityMismatch {
43 function: ctx.fragment.clone(),
44 expected: 0,
45 actual: args.len(),
46 });
47 }
48
49 let millis = ctx.runtime_context.clock.now_millis() as i64;
50 let row_count = ctx.row_count.max(1);
51 let data = vec![millis; row_count];
52 let bitvec = vec![true; row_count];
53
54 let result_data = ColumnData::int8_with_bitvec(data, bitvec);
55 Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), result_data)]))
56 }
57}