Skip to main content

reifydb_routine/function/clock/
now.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::data::ColumnData;
5use reifydb_type::value::r#type::Type;
6
7use crate::function::{
8	ScalarFunction, ScalarFunctionContext,
9	error::{ScalarFunctionError, ScalarFunctionResult},
10	propagate_options,
11};
12
13pub struct Now;
14
15impl Now {
16	pub fn new() -> Self {
17		Self {}
18	}
19}
20
21impl ScalarFunction for Now {
22	fn scalar(&self, ctx: ScalarFunctionContext) -> ScalarFunctionResult<ColumnData> {
23		if let Some(result) = propagate_options(self, &ctx) {
24			return result;
25		}
26
27		let row_count = ctx.row_count;
28
29		if ctx.columns.len() != 0 {
30			return Err(ScalarFunctionError::ArityMismatch {
31				function: ctx.fragment.clone(),
32				expected: 0,
33				actual: ctx.columns.len(),
34			});
35		}
36
37		let millis = ctx.runtime_context.clock.now_millis() as i64;
38		let data = vec![millis; row_count];
39		let bitvec = vec![true; row_count];
40
41		Ok(ColumnData::int8_with_bitvec(data, bitvec))
42	}
43
44	fn return_type(&self, _input_types: &[Type]) -> Type {
45		Type::Int8
46	}
47}